// component/navbar/navbar.js
var app = getApp();
Component({
  properties: {
    optsData: {
      type: Object,
      value: null,
      observer: function(newVal, oldVal) {
        console.log(newVal)
        if (newVal && JSON.stringify(newVal) != '{}') {
          var selectedIdx = newVal.item.selectedIdx || 0;
          var sliderOffset;
          if (newVal.item.margin) {
            // 这里是获取偏移量
            sliderOffset = (newVal.item.width + newVal.item.margin * 2) / app.globalData.pixelRatio * selectedIdx;
          } else {
            sliderOffset = newVal.item.width / app.globalData.pixelRatio * selectedIdx;
          }
          var scrollLeft = 0;
          console.log(app.globalData)
          if (sliderOffset > app.globalData.pageWidth / 2) {
            scrollLeft = sliderOffset - app.globalData.pageWidth / 2 + 30;
          }
          this.setData({
            activeIndex: selectedIdx,
            sliderOffset: sliderOffset,
            scrollLeft: scrollLeft,
          })
        }
      }
    },
    showNext: {
      type: Number,
      value: 0,
      observer: function(newVal, oldVal) {
        if (newVal == -1) {
          this.forward();
        } else if (newVal == 1) {
          this.next();
        }
        this.setData({
          showNext: 0
        })
      }
    }
  },
  data: {
    activeIndex: 0,
    sliderOffset: 0,
    scrollLeft: 0,
    navItem: [],
  },
  methods: {
    /**
     * 生成navItem
     */
    buildNavItem: function() {

    },

    /**
     * 头部tabbar点击事件
     */
    tabClick: function(e) {
      var index = e.currentTarget.id;
      if (index != this.data.activeIndex) {
        this.slide(index);
      }
    },

    /**
     * 显示后一个
     */
    next: function() {
      var index = this.data.activeIndex;
      if (index < this.data.optsData.data.length - 1) {
        index++;
        this.slide(index);
      }
    },

    /**
     * 显示前一个
     */
    forward: function() {
      var index = this.data.activeIndex;
      if (index > 0) {
        index--;
        this.slide(index);
      }
    },

    /**
     * 滑动
     */
    slide: function(index) {
      var sliderOffset
      if (this.data.optsData.item.margin) {
        sliderOffset = (this.data.optsData.item.width + this.data.optsData.item.margin * 2) / app.globalData.pixelRatio * index;
      } else {
        sliderOffset = this.data.optsData.item.width / app.globalData.pixelRatio * index;
      }
      var scrollLeft = 0;
      if (sliderOffset > app.globalData.pageWidth / 2) {
        scrollLeft = sliderOffset - app.globalData.pageWidth / 2 + 30;
      }
      this.setData({
        activeIndex: index,
        sliderOffset: sliderOffset,
        scrollLeft: scrollLeft
      })

      //组件通信，通知上级页面
      var myEventDetail = {
        id: this.data.optsData.data[index].id,
        type: this.data.optsData.data[index].type,
        index: index
      } // detail对象，提供给事件监听函数
      var myEventOption = {} // 触发事件的选项
      this.triggerEvent('tabclick', myEventDetail, myEventOption)
    }
  }
})